package com.fivefu.core.camera.controller;


import com.alibaba.fastjson.JSON;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.camera.services.DbMonitorCameraService;
import com.fivefu.core.camera.vo.response.CameraDistributeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "监控云图管理")
@RestController
@RequestMapping("/db-monitor-cloud-map")
public class DbMonitorCloudMapController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(DbMonitorCameraController.class);

    @Autowired
    private DbMonitorCameraService dbMonitorCameraService;

    @ApiOperation(value = "根据探头编号获取监控点位",httpMethod = "POST",response = CameraDistributeVo.class,notes = "根据探头编号获取监控点位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraIds",value = "探头主键集合,多个用,分隔",paramType = "query",required = false, dataType = "String")
    })
    @RequestMapping(path = "/queryMonitorPoint",method = RequestMethod.POST)
    public ResultInfo queryMonitorPoint(String cameraIds){
        try {
            logger.info("根据探头编号获取监控点位接收数据：" + JSON.toJSONString(request.getParameterMap()));
            List<Long> cameraIDs = new ArrayList<>();
            if (StrUtils.isNotNull(cameraIds)){
                String[] arr = cameraIds.split(",");
                for (int i = 0 ; i < arr.length ; i ++){
                    if (StrUtils.isNotNull(arr[i])) {
                        cameraIDs.add(Long.parseLong(arr[i]));
                    }
                }
            }
            List<CameraDistributeVo> list = dbMonitorCameraService.queryMonitorPoint(cameraIDs);
            return ResultInfo.renderSuccess(list);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询监控点位出错");
    }

    @ApiOperation(value = "监控点详情",httpMethod = "POST",response = ResultInfo.class,notes = "监控点详情")
    @RequestMapping(path = "/queryMonitorPointDetail",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraId",value = "探头主键",paramType = "query",required = true, dataType = "Long")
    })
    public ResultInfo queryMonitorPointDetail(Long cameraId){
        try {
            logger.info("监控点详情接收数据：" + JSON.toJSONString(request.getParameterMap()));
            if (null == cameraId) {
                return ResultInfo.renderError("探头id不能为空");
            }
            Map<String,Object> detail = dbMonitorCameraService.queryMonitorPointDetail(cameraId);
            return ResultInfo.renderSuccess(detail);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询监控详情出错");
    }

    @ApiOperation(value = "获取探头云图点位分布及数量",httpMethod = "POST",response = CameraDistributeVo.class,notes = "获取探头云图点位分布及数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraName",value = "探头名称",paramType = "query",required = false, dataType = "String")
    })
    @PostMapping(path = "/getCameraCloudDistribute")
    public ResultInfo getCameraCloudDistribute(String cameraName) {
        try {
            logger.info("获取探头云图点位分布及数量接收数据：" + JSON.toJSONString(request.getParameterMap()));
            Map<String,Object> resultMap = dbMonitorCameraService.getCameraCloudDistribute(cameraName);
            return ResultInfo.renderSuccess(resultMap);
        } catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("获取探头云图点位分布及数量");
    }
}
